package com.creditease.mobile.rss.ui.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.creditease.mobile.R;
import com.creditease.mobile.rss.model.RssItemModel;
import com.creditease.mobile.util.LruCacheUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RssItemAdapter extends BaseAdapter {

    private static final String TAG = RssItemAdapter.class.getName();

    private Context context;

    private List<RssItemModel> list;

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public RssItemAdapter(Context context, List<RssItemModel> list) {
        super();
        this.context = context;
        this.list = list;
    }

    private SimpleDateFormat sdfDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = LayoutInflater.from(context).inflate(R.layout.rss_item, null);
        RssItemModel itemModel = list.get(position);
        TextView mTextView = (TextView) convertView.findViewById(R.id.title);
        TextView timeView = (TextView) convertView.findViewById(R.id.time);
        ImageView logo = (ImageView) convertView.findViewById(R.id.list_item_pic);

        mTextView.setText(itemModel.articleTitle);
        timeView.setText(sdfDateFormat.format(new Date(itemModel.articleTime)));
        String key = "itemModel" + itemModel.isRead;
        // 已读
        if (itemModel.isRead) {
            //Log.d(TAG, itemModel.isRead + " ====");
            mTextView.setTextColor(R.color.solid_black);
            timeView.setTextColor(R.color.bg_gray);
            Bitmap bitmap = LruCacheUtil.getBitmapFromMemCache(key);
            if (bitmap == null) {
                bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rss_list_offline);
                LruCacheUtil.addBitmapToCache(key, bitmap);
            }
            logo.setImageBitmap(bitmap);
        } else {
            //Log.d(TAG, itemModel.isRead + "---");
            mTextView.setTextColor(Color.BLACK);
            timeView.setTextColor(Color.RED);
            Bitmap bitmap = LruCacheUtil.getBitmapFromMemCache(key);
            if (bitmap == null) {
                bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.rss_list_online);
                LruCacheUtil.addBitmapToCache(key, bitmap);
            }
            logo.setImageBitmap(bitmap);
        }

        return convertView;
    }
}
